$(function(){
	var stuGrid = $("#projectResourceGrid");
	var pid=$("#pid").val();
	//初始化
	stuGrid.datagrid({
		url:'resourcelist/'+pid,
		fit:true,
		rownumbers : true,
		fitColumns:true,
		border:false,
		pagination : true,// 是否显示分页栏
		columns:[[{
			field:'Project.title',
			title:'项目名称',
			width:90,
			align:'center',
			formatter: function (value,rec) {
		         return rec.project.title;
		     }
	
		},{
			field:'address',
			title:'资料地址',
			width:100,
			align:'center'
		},{	
			field:'student',
			title:'上传人',
			width:70,
			align:'center',
			formatter: function (value,rec) {
		         return rec.student.name;
		     }
		},{
			field:'type',
			title:'类型',
			width:70,
			align:'center'
		},{
			field: 'ac',
			title: '文件下载',
			width: 150,
			align: 'center',
			formatter: function(value, row, index) {
				var bts = [];	
				bts.push('<a data-index="' + index + '" class="ctr" href="download/' + row.id +'" style="font-size:12px;color:blue;cursor:pointer;">点我下载</a>');
				return bts.join('');
			}
		}]],
		toolbar:[ {
		    	 iconCls:'icon-add',
		    	 text:"上传项目资料",
		    	 handler:function(){
		    		 resourceForm(pid);
		    	 }
		     }
		  ]
	});
	
	var gridPanel = stuGrid.datagrid("getPanel");
	gridPanel.on("click", "a.download", function() {
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = stuGrid.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		alert("id为："+id);

		$.post("download/" + id , function(rs) {
			
		});
		
	});
	
	
	function resourceForm(pid) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href: "right_resource/"+pid,
			title: "上传项目资料",
			height: 370,
			width: 550,
			modal: true,
			onClose: function() {
				$(this).dialog("destroy");
			},
			buttons: [{
				iconCls: 'icon-save',
				text: '保存',
				handler: function() {
					$("#saveResource").form('submit', {
                        url: 'saveResource',
                        data: $("#saveResource").serialize(),success:function(success) {
                        	if(success){
                        		//刷新数据表格
                        		stuGrid.datagrid("reload");
            					//关闭窗口
                        		dialog.dialog("close");
                        	}
                        }
                    });	
				}
			}, {
				iconCls: 'icon-cancel',
				text: '取消',
				handler: function() {
					//关闭窗口
					dialog.dialog("close");
				}
			}]

		});
	}
});